package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.domian.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;

    //登录
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request) {
        //1.判断用户名是否存在
//        QueryWrapper<Employee> queryWrap = new QueryWrapper<>();
//        QueryWrapper<Employee> wrap = queryWrap.eq("username", employee.getUsername());
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Employee> wrap = lambdaQueryWrapper.eq(Employee::getUsername, employee.getUsername());
        //result为从数据库查到的数据
        Employee result = employeeService.getOne(wrap);
        if (result == null) {
            return R.error("用户名不存在");
        }
        //2.判断密码是否正确
        String password2 = employee.getPassword();//用户的密码
        String password1 = result.getPassword();//数据库密码
        //给密码加密
        String password3 = DigestUtils.md5DigestAsHex(password2.getBytes());
        //进行匹对
        if (!StringUtils.equals(password1, password3)) {
            return R.error("密码错误");
        }
        //3.判断用户状态是否被禁用
        if (result.getStatus() == 0) {
            return R.error("状态已被禁用");
        }
        //将数据存入Session域中
        request.getSession().setAttribute("employee", result.getId());
        return R.success(result);
    }

    //退出
    @PostMapping("/logout")
    public R<String> login(HttpServletRequest request) {
        request.getSession().invalidate();
        return R.success("退出成功");
    }

    //新增
    @PostMapping
    public R<String> create(@RequestBody Employee employee, HttpServletRequest request) {
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        long i1d = Thread.currentThread().getId();
        System.out.println("表现层id = " + i1d);
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        Long userId = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(userId);
//        employee.setUpdateUser(userId);
        employeeService.save(employee);
        return R.success("添加成功");
    }

    //分页查询
    @GetMapping("page")
    public R<Page<Employee>> page(Integer page, Integer pageSize, String name) {
        //分页查询对象
        Page<Employee> pageInfo = new Page<>(page, pageSize);
        //条件过滤对象
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //动态模糊查询,当name不为空时,进行查询, name为空此行代码无效
        queryWrapper.like(StringUtils.isNotBlank(name), Employee::getName, name);
        //排序
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }


    //禁用
    @PutMapping
    public R<String> update(@RequestBody Employee employee, HttpServletRequest request) {
        Long employeeId = (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(employeeId);
        employee.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(employee);
        return R.success("修改成功");
    }


    //回显 根据id查询
    @GetMapping("/{id}")
    public R<Employee> serviceById(@PathVariable Long id) {
        Employee employee = employeeService.getById(id);
        long i1d = Thread.currentThread().getId();
        System.out.println("表现层id = " + i1d);
        return R.success(employee);
    }
}
